package com.qinglei.recoup.longconn.service;

import com.qinglei.recoup.common.domain.CommonConstant;
import com.qinglei.recoup.common.domain.RadarMessage;
import com.qinglei.recoup.common.domain.RadarUserDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j    // implements InitializingBean
public class RadarDataTest{

    @Autowired
    private RadarDataV4_1Service radarDataV4_1Service;

    @Resource
    RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.host:}")
    private String rabbitmqHost;

    //@Override
    public void afterPropertiesSet() throws Exception {
        int i = 0;
        while (true){
            i++;
            Thread.sleep(1000);
            RadarMessage radarMessage = new RadarMessage();
            radarMessage.setSn("483FDA9C8478");
            radarMessage.setType(1);
            radarMessage.setTimestamp(System.currentTimeMillis());
            radarMessage.setProCode("3_0");
            RadarUserDataDTO radarUserDataDTO = new RadarUserDataDTO();

            if(i<7){
                radarUserDataDTO.setBreathBpm(26);
            }else {
//               if(i%19==0){
//                   radarUserDataDTO.setBreathBpm(26);
//
//               }else {
//                   radarUserDataDTO.setBreathBpm(5);
//               }
                radarUserDataDTO.setBreathBpm(5);
            }

            radarUserDataDTO.setHeartBpm(67);
            radarUserDataDTO.setS1(13);
            radarUserDataDTO.setS2(1);
            int[] breathPhaseData = {32766, 32768, 32770, 32771, 32772, 32772, 32771, 32770, 32769, 32767} ;
            radarUserDataDTO.setBreathPhaseData(breathPhaseData);
            int[] heartPhaseData = {32751, 32738, 32798, 32793, 32738, 32778, 32763, 32732, 32789, 32771};
            radarUserDataDTO.setHeartPhaseData(heartPhaseData);
            radarMessage.setData(radarUserDataDTO);
            this.sendMessage2MQ(radarMessage);
        }

    }

    /**
     * 发送消息到消息队列
     *
     * @param radarMessage
     */
    private void sendMessage2MQ(RadarMessage radarMessage) {
        if (StringUtils.isNotEmpty(rabbitmqHost)) {
            log.debug("sendMessage2MQ radarMessage: {}", radarMessage);
            rabbitTemplate.convertAndSend(CommonConstant.MQ_RECOUP_DIRECT_EXCHANGE, CommonConstant.MQ_RADAR_DATA_ROUTING, radarMessage);
        }
    }
}
